package cn.itcast.ks.service.impl;

import cn.itcast.ks.common.AppHttpCodeEnum;
import cn.itcast.ks.common.ResponseResult;
import cn.itcast.ks.common.threadlocal.UserThreadLocalUtils;
import cn.itcast.ks.mapper.ClassMapper;
import cn.itcast.ks.mapper.UserMapper;
import cn.itcast.ks.pojo.Vos.UserVo;
import cn.itcast.ks.pojo.dtos.AddIntegralDto;
import cn.itcast.ks.pojo.dtos.UserDto;
import cn.itcast.ks.pojo.entity.User;
import cn.itcast.ks.service.IUserService;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.org.apache.bcel.internal.generic.IFNULL;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mmff
 * @since 2021-05-03
 */
@Slf4j
@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {


    @Override
    public ResponseResult findByClassId() {

        User user = UserThreadLocalUtils.getUser();
        Integer classId = user.getClassId();

        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(classId != null, User::getClassId, classId)
                .eq(User::getRoleId, 2);
        List<User> users = this.list(lqw);

        List<UserVo> userVos = users.stream().map((u) -> {
            UserVo userVo = new UserVo();
            BeanUtils.copyProperties(u, userVo);
            userVo.setGroupsIntegral(u.getIntegral());
            return userVo;
        }).collect(Collectors.toList());

        return ResponseResult.okResult(userVos);
    }

    @Override
    public ResponseResult addIntegral(AddIntegralDto dto) {
        User user = UserThreadLocalUtils.getUser();
        Integer classId = user.getClassId();

        Integer id = dto.getId();
        User user1 = this.getById(id);

        if (user1 == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        if (!classId.equals(user1.getClassId())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }

        user1.setIntegral(user1.getIntegral() + dto.getIntegral());

        this.updateById(user1);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult resetGScoreByGroupId(Integer classId) {

        User user = UserThreadLocalUtils.getUser();
        Integer id = user.getClassId();

        if (!classId.equals(id)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }

        LambdaUpdateWrapper<User> luw = new LambdaUpdateWrapper<>();
        luw.set(User::getIntegral, 0)
                .eq(User::getClassId, classId);
        this.update(luw);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
